//
//  NetworkManager.m
//  HxgProject
//
//  Created by hxg on 2018/3/26.
//  Copyright © 2018年 XMYY-19. All rights reserved.
//

#import "NetworkManager.h"
#import "AFHTTPSessionManager.h"

@interface NetworkManager()
{
    
}

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation NetworkManager

singleton_implementation(NetworkManager)

// POST请求
- (void)postURL:(NSString *)urlInfo parameters:(id)parameters success:(void (^)(id responseObject))successful
        failure:(void (^) (NSError *error, ParamtersJudgeCode  judgeCode))failure
{
    self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    //申明返回的结果是json类型
    self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    self.manager.requestSerializer=[AFJSONRequestSerializer serializer];
    self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"application/json",nil];
    
    [self.manager POST:urlInfo parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        successful(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error,RequestFailed);
    }];
}

// GET请求
- (void)getURL:(NSString *)urlInfo parameters:(id)parameters success:(void (^)(id responseObject))successful
        failure:(void (^) (NSError *error, ParamtersJudgeCode  judgeCode))failure
{
    self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    //申明返回的结果是json类型
    self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    self.manager.requestSerializer=[AFJSONRequestSerializer serializer];
    self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/json",@"application/json",nil];
    
    [self.manager GET:urlInfo
           parameters:parameters
             progress:^(NSProgress * _Nonnull downloadProgress) {
                 
             } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                 NSLog(@"responseObject===>%@",responseObject);
                 successful(responseObject);
             } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                 failure(error,RequestFailed);
             }];
}


#pragma mark -

- (AFHTTPSessionManager *)manager
{
    if (!_manager)
    {
        _manager  = [AFHTTPSessionManager manager];
    }
    return _manager;
}

@end
